import { ConfigService } from '@nestjs/config';
import { Episode } from '@libs/db/models/episode.model';
import { Controller, Get } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { InjectModel } from 'nestjs-typegoose';
import { Model } from 'mongoose';
import { Crud } from 'nestjs-mongoose-crud';
import { Course } from '@libs/db/models/course.model';
import { EnvEnum } from '@app/common/enum/envEnum';

@Controller('episodes')
@ApiTags('课时')
@Crud({
  model: Episode,
})
export class EpisodesController {
  constructor(
    @InjectModel(Episode) private readonly model: Model<Episode>,
    @InjectModel(Course) private readonly CourseModel: Model<Course>,
    private readonly configService: ConfigService,
  ) {}

  @Get('options')
  async getOptions() {
    const courses = (await this.CourseModel.find()).map((course) => ({
      label: course.name,
      value: course._id,
    }));
    const uploadUrl =
      this.configService.get('env')[
        process.env.NODE_ENV === 'produciton'
          ? EnvEnum.dev_upload
          : EnvEnum.pro_upload
      ];

    return {
      title: '课时管理',
      columns: [
        { label: 'ID', prop: '_id' },
        { label: '课时名称', prop: 'name' },
        {
          label: '所属课程',
          prop: 'course',
          select: true,
          selectData: courses,
        },
        {
          label: '课时视频',
          prop: 'file',
          upload: true,
          uploadUrl: uploadUrl,
          video: true,
        },
      ],
    };
  }
}
